package com.chen.controller;

import com.chen.pojo.Result;
import com.chen.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;


@Slf4j
@RestController
public class uploadController {
    /**
     * 本地磁盘上传方法
     *
     * @param name
     * @param age
     * @param file
     * @return
     * @throws IOException
     */
//    @PostMapping("/upload")
//    public Result upload(String name, Integer age, MultipartFile file) throws IOException {
//        log.info("接收参数+{}+{}+{}",name,age,file);
//        //保存文件操作
//        String originalFilename = file.getOriginalFilename();//获取文件名
//        log.info("文件名:{}",originalFilename);
//        String exstion=originalFilename.substring(originalFilename.lastIndexOf("."));
//        String newFileName= UUID.randomUUID().toString()+exstion;
//        file.transferTo(new File("D:\\javacode\\web-project\\chen-managerment\\src\\main\\resources\\images\\"+newFileName));
//        return Result.success();
//    }
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("接收文件:{}", file);
        //将文件交给阿里云服务器
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传成功:{}", url);
        return Result.success(url);

    }
}

